package cn.itsource.ymcc.tasks;

import cn.itsource.ymcc.constant.KillConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.KillActivity;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.mapper.KillActivityMapper;
import cn.itsource.ymcc.mapper.KillCourseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Component
@Slf4j
public class PublishKillTasks {
    @Autowired
    private KillActivityMapper killActivityMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private KillCourseMapper killCourseMapper;
    /**
     * 将秒杀数据发布到redis里面
     */
    @Scheduled(cron = "0/5 * * * * *")//每天晚上2点执行
    public void publishKillCourse2Redis(){
        Date date = new Date();
        List<KillActivity> killActivities = killActivityMapper.selectList(new LambdaQueryWrapper<KillActivity>()
                .eq(KillActivity::getPublishStatus, RConst.Num.BASE_LONG_0)
                .le(KillActivity::getBeginTime, date)
                .ge(KillActivity::getEndTime, date));
        if(killActivities.size() == RConst.Num.INT_0){
            log.info("当前没有秒杀需求发布！");
            return;
        }
        List<Long> ids = killActivities.stream().map(KillActivity::getId).collect(Collectors.toList());
        //所有的秒杀课程
        List<KillCourse> killCourses = killCourseMapper.selectList(new QueryWrapper<KillCourse>().in("activity_id",ids));
        /**
         * 修改秒杀课程的状态
         */
        Map<String,KillCourse>  map = new HashMap<>();
        for (KillCourse killCours : killCourses) {
            //首先去找 库存
            redisTemplate.opsForValue()
                    .set(KillConstants.KILL_STORE+killCours.getCourseId(),killCours.getKillCount());
            //课程列表
            map.put(killCours.getId().toString(),killCours);
            //将课程修改为已发布
            killCours.setPublishStatus(RConst.Num.INT_1);
            killCours.setPublishTime(date);
            //发布进行修改
            killCourseMapper.updateById(killCours);
        }
        /**
         * 修改秒杀活动的状态
         */
        killActivities.forEach(killActivity -> {
            killActivity.setPublishStatus(RConst.Num.INT_1);
            killActivity.setPublishTime(date);
            killActivityMapper.updateById(killActivity);
        });


        BoundHashOperations<String, Object, Object> hashOps = redisTemplate.boundHashOps(KillConstants.KEY_KILL_COURSE);
        //将数据添加到 hash对象里面去
        hashOps.putAll(map);
    }
}
